<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>菜品类别编辑</title>
    <link href="https://cdn.bootcss.com/twitter-bootstrap/3.0.1/css/bootstrap.css" rel="stylesheet">
    <link rel="stylesheet" th:href="@{/layui/css/layui.css}">
    <i th:replace="~{common :: baseUrl}"></i>
</head>
<body>
<form class="layui-form layui-form-pane" action=""  lay-filter="example">


    <div class="container">
        <div class="row clearfix">
            <div class="col-md-6 column">

                <div class="layui-form-item">
                    <label class="layui-form-label">菜品编号</label>
                    <div class="layui-input-inline">
                        <input type="text" name="id" lay-verify="required" th:value="${foodRecord.id}" placeholder="请输入" autocomplete="off" class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">菜品名称</label>
                    <div class="layui-input-inline">
                        <input type="text" name="foodName" lay-verify="required"  th:value="${foodRecord.foodName}" placeholder="请输入" autocomplete="off" class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">菜品类别</label>
                    <div class="layui-input-inline">
                        <select name="levelOne" id="levelOne" lay-filter="levelOne"  >
                            <option value=''>请选择菜品类别</option>
                            <option th:each="f : ${map.parents}"  th:value="${f.id}" th:text="${f.parentName}" th:selected="${foodRecord.categoryParentId eq f.id}"></option>
                        </select>
                    </div>
                    <div class="layui-input-inline">
                        <select name="categoryId" id="levelTwo">
                            <option value=''>请选择分类名称</option>
                            <option th:each="f : ${map.childs}"  th:value="${f.id}" th:text="${f.child}"  th:selected="${foodRecord.categoryId == f.id}"></option>
                        </select>
                    </div>

                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">价格</label>
                    <div class="layui-input-inline">
                        <input type="text" name="priceString"   th:value="${foodRecord.priceString}" lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input">
                    </div>
                </div>


                <div class="layui-form-item">
                    <label class="layui-form-label">特价</label>
                    <div class="layui-input-inline">
                        <input type="text" name="specialPrice"   th:value="${foodRecord.specialPrice}" lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">生效时间</label>
                        <div class="layui-input-inline" style="width: 160px;">
                            <input type="text" class="layui-input" name="specialPriceStartDate" th:value="${foodRecord.specialPriceStartDate}" id="startDate" placeholder="yyyy-MM-dd HH:mm:ss">
                        </div>
                        <div class="layui-form-mid">-</div>
                        <div class="layui-input-inline" style="width: 160px;">
                            <input type="text" class="layui-input" id="endDate" name="specialPriceEndDate" th:value="${foodRecord.specialPriceEndDate}" placeholder="yyyy-MM-dd HH:mm:ss">
                        </div>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">库存</label>
                    <div class="layui-input-inline">
                        <input type="text" name="repertory"   th:value="${foodRecord.repertory}" lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">库存范围</label>
                        <div class="layui-input-inline" style="width: 160px;">
                            <input type="text" class="layui-input" name="repertoryLowLine"  th:value="${foodRecord.repertoryLowLine}"  placeholder="库存过低预警线">
                        </div>
                        <div class="layui-form-mid">-</div>
                        <div class="layui-input-inline" style="width: 160px;">
                            <input type="text" class="layui-input"  name="repertoryHighLine" th:value="${foodRecord.repertoryHighLine}" placeholder="库存超出预警线">
                        </div>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">排序</label>
                    <div class="layui-input-inline">
                        <input type="text" name="orders"   th:value="${foodRecord.orders}" lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item layui-form-text">
                    <label class="layui-form-label">说明</label>
                    <div class="layui-input-block">
                        <textarea placeholder="请输入内容" name="explains" th:text="${foodRecord.explains}" class="layui-textarea"></textarea>
                    </div>
                </div>


            </div>
            <div class="col-md-6 column">

                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">上传图片</label>
                        <div class="layui-input-inline">
                            <div class="layui-upload">

                                <div class="layui-upload-list">
                                    <input hidden name="picPath" id="picPath" th:value="${foodRecord.picPath}">
                                    <img width="100px" height="100px" class="layui-upload-img" name="showPic" id="showPic" th:src="${foodRecord.picPath}">
                                    <p id="uploadText"></p>
                                </div>
                                <button type="button" class="layui-btn" id="uploadPic">上传图片</button>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">商品状态</label>
                    <div class="layui-input-block">
                        <input type="checkbox"  th:checked="${foodRecord.status == '1'}"  name="status" value="1" lay-skin="switch"  lay-text="上架|下架">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">打印标签</label>
                    <div class="layui-input-block">
                        <input type="checkbox"  th:checked="${foodRecord.printLabel == '1'}"  name="printLabel" value="1" lay-skin="switch" lay-text="开启|关闭">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">手动打折</label>
                    <div class="layui-input-block">
                        <input type="checkbox"  th:checked="${foodRecord.manualDiscount == '1'}"  name="manualDiscount" value="1" lay-skin="switch" lay-text="开启|关闭">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">计重</label>
                    <div class="layui-input-block">
                        <input type="checkbox"  th:checked="${foodRecord.weight == '1'}" name="weight" value="1" lay-skin="switch"  lay-text="开启|关闭">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">自动冲减原料</label>
                    <div class="layui-input-block">
                        <input type="checkbox" th:checked="${foodRecord.automaticOffset == '1'}"  name="automaticOffset" value="1" lay-skin="switch" lay-filter="switchTest" lay-text="开启|关闭">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">实价菜</label>
                    <div class="layui-input-block">
                        <input type="checkbox"  th:checked="${foodRecord.firmOffers == '1'}" name="firmOffers" value="1" lay-skin="switch" lay-filter="switchTest" lay-text="开启|关闭">
                    </div>
                </div>
            </div>
        </div>
    </div>







    <div class="layui-form-item" align="center">
        <div class="layui-input-block">
            <button type="submit" class="layui-btn" lay-submit="" lay-filter="sub">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>

</form>



</body>
<script th:src="@{/layui/layui.js}"></script>
<script th:inline="none">
    //JavaScript代码区域
    layui.use(['element','form','upload','layer','laydate'], function() {
        var element = layui.element;
        var form = layui.form;
        var upload = layui.upload;
        var layer = layui.layer;
        var laydate = layui.laydate;
        var $ = layui.$;


        //日期时间选择器
        laydate.render({
            elem: '#startDate'
            ,type: 'datetime'
        });
        //日期时间选择器
        laydate.render({
            elem: '#endDate'
            ,type: 'datetime'

        });

        //监听一级分类下拉列表的改变
        form.on('select(levelOne)', function(data){
            // console.log(data.elem); //得到select原始DOM对象
            // console.log(data.value); //得到被选中的值
            // console.log(data.othis); //得到美化后的DOM对象
            var parentId = data.value;

            var content="<option value=''>请选择分类名称</option>";

            if(parentId == null||parentId == ""){

                $("#levelTwo").html(content);
                form.render('select');
                return;
            }

            $.getJSON(
                baseUrl+"food/record/typeLevel",
                {parentId:parentId},
                function (result) {
                    if (result.code==0){

                        var jsonArrays = result.data;

                        for (var i = 0;i<jsonArrays.length;i++){
                            content+="<option value='"+jsonArrays[i].id+"'>"+jsonArrays[i].child+"</option>"
                        }
                        $("#levelTwo").html(content);
                        form.render('select');
                    }else {
                        layer.msg(result.msg);
                    }
                }
            )

        });


        //图片上传
        var uploadInst = upload.render({
            elem: '#uploadPic'
            ,url: baseUrl+'upload/pic'
            ,before: function(obj){
                //预读本地文件示例，不支持ie8
                obj.preview(function(index, file, result){
                    $('#showPic').attr('src', result); //图片链接（base64）
                });
            }
            ,done: function(res){
                //如果上传失败
                if(res.code != 0){
                    return layer.msg('上传失败');
                }else {
                    $("#picPath").val(res.data.src)
                }
                //上传成功
            }
            ,error: function(){
                //演示失败状态，并实现重传
                var demoText = $('#uploadText');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function(){
                    uploadInst.upload();
                });
            }
        });




        //提交
        form.on('submit(sub)', function(data){

            //发送ajax请求  编辑APP信息
            $.ajax({
                url:baseUrl+"food/record/edit",
                data:data.field,
                type:"post",
                dataType:"json",
                success:function (result) {
                    if (result.code==0){
                        layer.open({
                            title: '编辑菜品信息'
                            ,content: '恭喜你! 编辑菜品信息成功！'
                            ,end:function () {
                                //当你在iframe页面关闭自身时
                                var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                                parent.layer.close(index); //再执行关闭
                            }
                        });
                    }else {
                        layer.open({
                            title: '编辑菜品信息'
                            ,content: result.msg
                        });
                    }
                },
                error:function () {
                    layer.msg("服务器原地爆炸,请联系管理员!!!");
                }
            })
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。

        });





    });

</script>
</html>